package com.wuxianbei.wuxianbei.controller;

import com.wuxianbei.wuxianbei.pojo.Result;
import com.wuxianbei.wuxianbei.pojo.Team;
import com.wuxianbei.wuxianbei.service.TeamService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import java.util.List;

@Slf4j
@RestController
@CrossOrigin
public class TeamController {

    @Autowired
    private TeamService teamService;

    /*
    *增添新的队伍信息
     */
    @RequestMapping(value = "/sign",method = RequestMethod.OPTIONS)
    public Result deleteOptions(){
        log.info("OPTIONS已被处理");
        return Result.success();
    }

    @CrossOrigin
    @RequestMapping(value = "/sign",method = RequestMethod.POST)
    public Result add(@RequestBody Team team){

        log.info("新增数据：{}",team);
        List<Team> allList = teamService.all();
        for(int i= 0; i< allList.size(); i++){
            if(allList.get(i).getLeaderNumber().equals(team.getLeaderNumber())){
                teamService.delete(team.getLeaderNumber());
                log.info("已有数据");
            }
        }
        //调用Sercive增加学生信息
        teamService.add(team);
        return Result.success();
    }


    /*
     *根据学号查询报名信息
     */
    @GetMapping ( "/inquire")
    public Result list(String leaderNumber){
        log.info("查询队长学号为"+leaderNumber+"的队伍");
        //调用Sercive查询学生信息
        List<Team> teamList = teamService.list(leaderNumber);

        return Result.success(teamList);
    }

    /*
     *内部人员查看所有报名队伍
     */
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public Result all(){
        log.info("查询所有报名队伍");
        //调用Sercive查询学生信息
        List<Team> allList = teamService.all();

        return Result.success(allList);
    }

    /*
    *根据不同条件筛选
     */
    @GetMapping("/keys")
    public Result searchByKeys(String key,String val){
        log.info("根据不同条件查询报名信息");
        log.info(key+val);
        List<Team> teamList = null;
        //调用Sercive查询学生信息
        if(key.equals("topic")){
            teamList = teamService.searchByTopic(val);
        }else if(key.equals("teamName")){
            teamList = teamService.searchByTName(val);
        }else if(key.equals("leaderName")){
            teamList = teamService.searchByLeName(val);
        }else if(key.equals("leaderNumber")){
            teamList = teamService.searchByLeNumber(val);
        }

        return Result.success(teamList);
    }

    @GetMapping("/topic")
    public Result topic(String leaderNumber,String topic){
        teamService.topic(leaderNumber,topic);
        return Result.success();
    }
}
